package com.xhwl.centiotdata.pollingtrigger.vehicle;

import com.xhwl.centiotdata.handler.vehicle.IVehicleIntegrationHandler;
import com.xhwl.centiotdata.pojo.enums.vehicle.VehicleHandlerEnum;
import com.xhwl.centiotdata.pollingtrigger.ScheduleDispatcher;
import com.xhwl.centiotdata.service.integrateconfig.VehicleIntegrationConfig;
import com.xhwl.centiotdata.utils.ThreadPoolUtils;
import com.xhwl.common.pojo.cent.vehicle.bo.config.VehicleIntegrationConfigBO;
import com.xhwl.common.pojo.cent.vehicle.bo.http.ParkingLotAccessRecordForInBO;
import com.xhwl.common.pojo.cent.vehicle.bo.http.ParkingLotAccessRecordForOutBO;
import com.xhwl.common.pojo.cent.vehicle.bo.http.ParkingLotPayRecordBO;
import com.xhwl.common.pojo.cent.vehicle.bo.http.query.QueryAccessRecordBO;
import com.xhwl.common.pojo.cent.vehicle.converter.feign.AccessRecordFeignServiceConverter;
import com.xhwl.common.pojo.cent.vehicle.converter.http.HttpQueryParameterConverter;
import com.xhwl.common.utils.vehicle.AboutDateUtil;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class VehicleDispatcher implements ScheduleDispatcher {

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private AccessRecordFeignServiceConverter accessRecordFeignServiceConverter;

    @Autowired
    private VehicleIntegrationConfig integrationConfig;

    @Override
    public void doOneHourTask() {
        //车行数据
        ThreadPoolUtils.executor.execute(this::executePullAccessRecord);
    }

    public void executePullAccessRecord() {

        Map<Integer, List<VehicleIntegrationConfigBO>> vehicleIntegrationConfigMap = integrationConfig.getIntegrationConfigs();
        //由于一个项目只支持一个集成配置，因此可以不使用循环逻辑
        vehicleIntegrationConfigMap.forEach((key, value) -> {

            if (1 != value.size()) {
                log.error("该项目的集成配置具有多个，暂时项目的集成配置只要一个项目配置一个集成配置");
            }

            value.forEach(vehicleIntegrationConfigBO -> {

                try {
                    String serviceName = VehicleHandlerEnum.toVehicleHandlerEnum(vehicleIntegrationConfigBO.getVehicleSystemInfoBO().getProductId()).getServiceName();
                    IVehicleIntegrationHandler vehicleIntegrationHandler = applicationContext.getBean(serviceName, IVehicleIntegrationHandler.class);

                    Date endTime = AboutDateUtil.theSameHourOfDate(new Date(), "UTC+8");
                    Date startTime = AboutDateUtil.dateMinusHour(endTime, 1);
                    //调用海康接口获取车辆的入场的记录
                    ParkingLotAccessRecordForInBO parkingLotAccessRecordForInBO = vehicleIntegrationHandler.queryAccessRecordForIn(vehicleIntegrationConfigBO, new QueryAccessRecordBO(startTime, endTime, 0));
                    //log.info("parkingLotAccessRecordForInBO: " + parkingLotAccessRecordForInBO);

                    //调用车行feign接口，把入场记录存储到车行中台
                    vehicleIntegrationHandler.addAccessRecordsForIn(accessRecordFeignServiceConverter.toCreateVehicleAccessRecordsForInFeignBO(parkingLotAccessRecordForInBO));

                    //调用海康接口获取车辆的出场的记录
                    ParkingLotAccessRecordForOutBO parkingLotAccessRecordForOutBO = vehicleIntegrationHandler.queryAccessRecordForOut(vehicleIntegrationConfigBO, new QueryAccessRecordBO(startTime, endTime, 1));
                    //log.info("parkingLotAccessRecordBOForOut: " + parkingLotAccessRecordForOutBO);

                    //调用车行feign接口，把出场记录存储到车行中台
                    vehicleIntegrationHandler.addAccessRecordsForOut(accessRecordFeignServiceConverter.toCreateVehicleAccessRecordsForOutFeignBO(parkingLotAccessRecordForOutBO));


                    //调用接口获取车辆的支付的记录
                    ParkingLotPayRecordBO parkingLotPayRecordBO = vehicleIntegrationHandler.queryPayRecord(vehicleIntegrationConfigBO, new QueryAccessRecordBO(startTime, endTime, 1));
                    //log.info("parkingLotPayRecordBO: " + parkingLotPayRecordBO);

                    //调用车行feign接口，把缴费记录存储到车行中台
                    vehicleIntegrationHandler.addPayRecords(parkingLotPayRecordBO);
                } catch (Exception e) {
                    log.error("该项目拉取车场缴费记录发生异常错误，异常信息为：" + e.getMessage());
                }

            });

        });

    }

}
